﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AddGroupNewsItem : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AddGroupNewsItem";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thêm mới chi tiết nhóm tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddGroupNewsItem));
            string GroupNewsId = (string)System.Web.HttpContext.Current.Request["GroupNewsId"];
            string ParentGroupNewsItemId = (string)System.Web.HttpContext.Current.Request["ParentGroupNewsItemId"];
            if (ParentGroupNewsItemId == null) ParentGroupNewsItemId = "";
            string SelectAllowRss = "<select id=drpSelectRss>" +
                                  "     <option selected value=0>Không cho phép hiện Rss</option> " +
                                  "     <option value=1>Cho phép hiện Rss</option> " +
                                  "</select>";
          

            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "GroupNews.js") +
                             Environments.GetFormDialog(
                             "      <table>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Alias", "Alias") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupNewsItemAlias></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "ShortSubject", "Tên ngắn") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtShortSubject></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "AddGroupNewsItemCaption", "Tên nhóm") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtGroupNewsItemName></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Description", "Mô tả") + "</td>" +
                             "              <td><textarea class=cssTextBoxDialog id=txtDescription></textarea></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "IndexOrderText", "Thứ tự") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtIndexOrder></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Rss", "Rss") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtRssUrl></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "JumpUrl", "Url") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtJumpUrl></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "ShowInMenuCaption", "Hiện thị menu") + "</td>" +
                             "              <td><input id=chkShowInMenu type=checkbox></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "IsGroupNews", "Nhóm tin ?") + "</td>" +
                             "              <td><input id=chkIsGroupNews type=checkbox></td>" +
                             "          </tr>" +
                              "          <tr>" +
                             "              <td>" + ResourceBLL.GetText(WebPartId, "Rss", "Rss") + "</td>" +
                             "              <td>"+SelectAllowRss+"</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddGroupNewsItem('" + GroupNewsId + "','" + ParentGroupNewsItemId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "AddGroupNewsItemSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "AddGroupNewsItemCancelButton", "Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>", ResourceBLL.GetText(WebPartId, "AddGroupNewsItemFormTitle", "Thêm chi tiết nhóm tin tức"));
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideAddGroupNewsItem(
            string GroupNewsId, 
            string GroupNewsItemName, 
            string Description, 
            string ParentGroupNewsItemId, 
            string IndexOrder, 
            string RssUrl, 
            string JumpUrl, 
            bool ShowInMenu, 
            bool IsGroupNews, 
            string GroupNewsItemAlias, 
            string ShortSubject,
            int AllowRss)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                if (GroupNewsItemName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tên nhóm!"));
                if (ParentGroupNewsItemId.Trim().Length == 0) ParentGroupNewsItemId = null;
                int LevelIndex = 0;
                if (ParentGroupNewsItemId != null)
                {
                    GroupNewsItemCls parentGroupNewsItemCls = new GroupNewsBLL().CreateModelItem(Environments.ActiveLanguage, ParentGroupNewsItemId);
                    LevelIndex = parentGroupNewsItemCls.LevelIndex;
                }
                string GroupNewsItemId = new GroupNewsBLL().AddGroupNewsItem(null, GroupNewsId, GroupNewsItemName, Description, ParentGroupNewsItemId, RssUrl, JumpUrl, int.Parse(IndexOrder), ShowInMenu ? 1 : 0, LevelIndex, IsGroupNews ? 1 : 0, GroupNewsItemAlias, ShortSubject, AllowRss);
                return ResourceBLL.GetText(WebPartId,"MessageAddOk","Thêm mới thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}